﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.OBJECT;
using System.Data.SqlClient;
using System.Data;

namespace QuanLyBanHang.DAL
{
    public class DalNhanVien
    {
        public static DataTable Load()
        {
            return DataHelper.Load(@"select NhanVien.MaNV,Ten,GioiTinh,NgaySinh,SoDienThoai,CMND,DiaChi,
                                    TaiKhoan,MatKhau,Email,TenQuyen from NhanVien inner join Users
                                    on NhanVien.MaNV = Users.MaNV inner join Quyen
                                    on Users.MaQuyen = Quyen.MaQuyen
                                    where TrangThai = 1");
        }

        public static DataTable Load_Quyen()
        {
            return DataHelper.Load("Select * from Quyen");
        }

        public static int Insert(ObjNhanVien obj)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[7];
                string[] names = {"MaNV","Ten","GioiTinh","NgaySinh","SoDienThoai","CMND","DiaChi"};
                object[] values = { obj.MaNV,obj.Ten,obj.GioiTinh,obj.NgaySinh,obj.SoDienThoai,obj.CMND,obj.DiaChi};
                for (int i = 0; i < para.Length; i++)
                {
                    para[i] = new SqlParameter(names[i], values[i]);
                }
                return DataHelper.Update(para, "spInsertNhanVien");
            }
            catch (Exception ex)
            { 
                throw ex;
            }
        }

        public static int Update(ObjNhanVien obj)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[7];
                string[] names = { "MaNV", "Ten", "GioiTinh", "NgaySinh", "SoDienThoai", "CMND", "DiaChi" };
                object[] values = { obj.MaNV, obj.Ten, obj.GioiTinh, obj.NgaySinh, obj.SoDienThoai, obj.CMND, obj.DiaChi };
                for (int i = 0; i < para.Length; i++)
                {
                    para[i] = new SqlParameter(names[i], values[i]);
                }
                return DataHelper.Update(para, "spUpdateNhanVien");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int Delete(string MaNV)
        {
            try
            {
                SqlParameter[] para = {new SqlParameter("MaNV",MaNV) };
                return DataHelper.Update(para ,"spDeleteNhanVien");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataTable GetID()
        {
            string sql = "select top 1 MaNV from NhanVien order by MaNV desc";
            DataTable dt = DataHelper.Load(sql);
            return dt;
        }
    }
}
